package org.edu.modules.classChoise.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONArray;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import lombok.SneakyThrows;
import org.apache.shiro.SecurityUtils;
import org.edu.common.api.vo.Result;
import org.edu.common.system.query.QueryGenerator;
import org.edu.common.system.vo.LoginUser;
import org.edu.modules.util.ExcelUtils;
import org.edu.common.util.oConvertUtils;
import org.edu.modules.classChoise.entity.VeJwXkdj;
import org.edu.modules.classChoise.entity.VeJwXkdj;
import org.edu.modules.classChoise.service.IVeJwXkdjService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.edu.common.system.base.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.aspect.annotation.AutoLog;

 /**
 * @Description: 选课登记
 * @Author:
 * @Date:   2021-05-14
 * @Version: V1.0
 */
@Api(tags="选课登记")
@RestController
@RequestMapping("/classChoise/veJwXkdj")
@Slf4j
public class VeJwXkdjController  extends BaseController<VeJwXkdj, IVeJwXkdjService> {
	@Autowired
	private IVeJwXkdjService veJwXkdjService;
	
	/**
	 * 分页查询
	 * @param veJwXkdj
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "选课登记-分页查询")
	@ApiOperation(value="选课登记-分页查询", notes="选课登记-分页查询")
	@PostMapping(value = "/list")
	public Result<?> queryPageList( VeJwXkdj veJwXkdj,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<VeJwXkdj> queryWrapper = QueryGenerator.initQueryWrapper(veJwXkdj, req.getParameterMap());
		Page<VeJwXkdj> page = new Page<VeJwXkdj>(pageNo, pageSize);
		IPage<VeJwXkdj> pageList = veJwXkdjService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 @AutoLog(value = "代选课-分页查询")
	 @ApiOperation(value="代选课-分页查询", notes="代选课-分页查询")
	 @PostMapping(value = "/dxklist")
	 public Result<?> queryDxkPageList( VeJwXkdj VeJwXkdj,
									@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									HttpServletRequest req) {

		 QueryWrapper<VeJwXkdj> queryWrapper = QueryGenerator.initQueryWrapper(VeJwXkdj, req.getParameterMap());

	/*	 Integer semid= VeJwXkdj.getSemid();//学期
		String xh= VeJwXkdj.getStunum();//学号
		 if(semid!=null&& !semid.equals("")) {
			 queryWrapper.eq("semid", semid);
		 }
		 if(xh!=null&& !xh.equals("")) {
			 queryWrapper.eq("stuNum", xh);
		 }
		 String kch= VeJwXkdj.getKch();//课程号
		 if(kch!=null && !kch.equals("")) {
			 queryWrapper.like("kch", kch);
		 }
		 String kcmc= VeJwXkdj.getKcmc();//课程名称
		 if(kcmc!=null && !kcmc.equals("")) {
			 queryWrapper.eq("kcmc", kcmc);
		 }

		 Integer falid= VeJwXkdj.getFalid();//专业部
				 if(falid!=null && !falid.equals("")) {
			 queryWrapper.eq("falid", falid);
		 }
		 Integer specid= VeJwXkdj.getSpecid();//专业
				 if(specid!=null && !specid.equals("")) {
			 queryWrapper.eq("specid", specid);
		 }
		 Integer bjid= VeJwXkdj.getBjid();//班级
		 if(bjid!=null && !bjid.equals("")) {
			 queryWrapper.eq("bjid", bjid);
		 }
*/
		 Page<VeJwXkdj> page = new Page<VeJwXkdj>(pageNo, pageSize);
		 IPage<VeJwXkdj> pageList = veJwXkdjService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }


	 @AutoLog(value = "我的选课-分页查询")
	 @ApiOperation(value="我的选课-分页查询", notes="我的选课-分页查询")
	 @PostMapping(value = "/myxklist")
	 public Result<?> queryMyxkPageList( VeJwXkdj VeJwXkdj,
									   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
									   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
									   HttpServletRequest req) {

		 QueryWrapper<VeJwXkdj> queryWrapper = QueryGenerator.initQueryWrapper(VeJwXkdj, req.getParameterMap());

		/* Integer semid= VeJwXkdj.getSemid();//学期
		 if(semid!=null&& !semid.equals("")) {
			 queryWrapper.eq("semid", semid);
		 }
		 Integer status= VeJwXkdj.getStatus();//状态
		 if(status!=null&& !status.equals("")) {
			 queryWrapper.eq("status", status);
		 }
		 String kch= VeJwXkdj.getKch();//课程号
		 if(kch!=null && !kch.equals("")) {
			 queryWrapper.like("kch", kch);
		 }
		 queryWrapper.clone()
		 String kcmc= VeJwXkdj.getKcmc();//课程名称
		 if(kcmc!=null && !kcmc.equals("")) {
			 queryWrapper.like("kcmc", kcmc);
		 }
	/*	 String kczh= VeJwXkdj.getKczh();//课程组合
		 if(kczh!=null && !kczh.equals("")) {
			 queryWrapper.like("kczh", kczh);
		 }*/

		/* Integer falid= VeJwXkdj.getFalid();//专业部
		 if(falid!=null && !falid.equals("")) {
			 queryWrapper.eq("falid", falid);
		 }
		 Integer specid= VeJwXkdj.getSpecid();//专业
		 if(specid!=null && !specid.equals("")) {
			 queryWrapper.eq("specid", specid);
		 }
		 Integer bjid= VeJwXkdj.getBjid();//班级
		 if(bjid!=null && !bjid.equals("")) {
			 queryWrapper.eq("bjid", bjid);
		 }


		 String stunum= VeJwXkdj.getStunum();//学生号
		 if(stunum!=null&& !stunum.equals("")) {
			 queryWrapper.eq("stuNum", stunum);
		 }*/
		 Page<VeJwXkdj> page = new Page<VeJwXkdj>(pageNo, pageSize);
		 IPage<VeJwXkdj> pageList = veJwXkdjService.page(page, queryWrapper);
		 return Result.OK(pageList);
	 }

	 /**
	* 通过id查询
	* @param id
	* @return
	*/
	@AutoLog(value = "选课登记-通过id查询")
	@ApiOperation(value="通过id查询", notes="选课登记-通过id查询")
	@PostMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		VeJwXkdj veJwXkdj = veJwXkdjService.getById(id);
		if(veJwXkdj==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(veJwXkdj);
	}



	/**
	 * 新增信息
	 * @param veJwXkdj
	 * @return
	 */
	@AutoLog(value = "选课登记-新增信息")
	@ApiOperation(value="新增信息", notes="选课登记-新增信息")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody VeJwXkdj veJwXkdj) {
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        if(sysUser!=null){
            veJwXkdj.setCreateuserid(sysUser.getId());
            veJwXkdj.setCreateusername(sysUser.getRealname());
        }
        veJwXkdj.setCreatetime(new Date());
		if(veJwXkdjService.save(veJwXkdj)){
		   return Result.OK("添加成功！",veJwXkdj.getId());
        }
        return Result.error("添加失败!");
	}
	
	/**
	 * 修改信息
	 * @param veJwXkdj
	 * @return
	 */
	@AutoLog(value = "选课登记-修改信息")
	@ApiOperation(value="修改信息", notes="选课登记-修改信息")
	@PostMapping(value = "/edit")
	public Result<?> edit(@RequestBody VeJwXkdj veJwXkdj) {
        veJwXkdj.setUpdatetime(new Date());
        if(veJwXkdjService.updateById(veJwXkdj)){
		    return Result.OK("编辑成功!");
        }
        return Result.error("编辑失败!");
	}
	
	/**
	 * 通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "选课登记-通过id删除")
	@ApiOperation(value="通过id删除", notes="选课登记-通过id删除")
	@PostMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        if(veJwXkdjService.removeById(id)){
		    return Result.OK("删除成功!");
        }
        return Result.error("删除失败!");
	}
	
	/**
	 * 批量删除
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "选课登记-批量删除")
	@ApiOperation(value="批量删除", notes="选课登记-批量删除")
	@PostMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		if(this.veJwXkdjService.removeByIds(Arrays.asList(ids.split(",")))){
		         return Result.OK("批量删除成功!");
        }
        return Result.error("批量删除失败!");
	}


	 /**
	  * 未分班的选课学生信息

	  * @return
	  */
	 @AutoLog(value = "未分班的选课学生信息")
	 @ApiOperation(value="未分班的选课学生信息", notes="未分班的选课学生信息-查询列表")
	 @PostMapping(value = "/wfbxkStuList")
	 public Result<?> wfbxkStuList(@RequestBody VeJwXkdj veJwXkdj,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req ) {
		 QueryWrapper<VeJwXkdj> queryWrapper = QueryGenerator.initQueryWrapper(veJwXkdj, req.getParameterMap());
		 Integer semid=veJwXkdj.getSemid();//学期id
		 if(semid!=null&& !semid.equals("")) {
			 queryWrapper.eq("l.semid", semid);
		 }
		 String kcid=veJwXkdj.getKch();//课程号
		 if(kcid!=null&& !kcid.equals("")) {
			 queryWrapper.eq("l.kcid", kcid);
		 }
		/* String stuName=veJwXkdj.getStuname();//学生姓名
		 if(stuName!=null&& !stuName.equals("")) {
			 queryWrapper.like("l.stuName", stuName);
		 }*/
		 Page<Map<String,Object>> page = new Page<Map<String,Object>>(pageNo, pageSize);
		 IPage<Map<String,Object>> pageList = veJwXkdjService.pageMaps(page, queryWrapper);
		 return Result.OK(pageList);
//		 List<Map<String, Object>> list = veJwXkdjService.selectMapsPage1(queryWrapper);
//		 return Result.OK(list);
	 }
	 @SneakyThrows
	 @ApiOperation("导入代选课")
	 // @GetMapping(value = "/importHeadQuestion")
	 @PostMapping("importDsk")
	 public Result importDsk(@RequestParam("file") MultipartFile file) throws IOException {
		 JSONArray jsonHeadDskinfo = ExcelUtils.excelToJson(file);
		 // 导入空文件
		 if (jsonHeadDskinfo == null || jsonHeadDskinfo.isEmpty()) {
			 return Result.error("不可导入空文件");
		 }
		 veJwXkdjService.importDskinfo(jsonHeadDskinfo);
		 return new Result();
	 }
	 /**
	  * 导出excel
	  *
	  * @param request
	  * @param veJwXkdj
	  */
	 @RequestMapping(value = "/exportXls")
	 public ModelAndView exportXls(HttpServletRequest request,   VeJwXkdj veJwXkdj) {
		 return super.exportXls(request, veJwXkdj, VeJwXkdj.class, "管理部门代选班");
	 }
}
